<?php
namespace app\wx\controller;

use think\Db;

class Notify
{
    public function wxgzh(){
        $msg = file_get_contents("php://input");
        $txt = date('YmdHis',time()).PHP_EOL.$msg;
        writeLog('jsapi_notify',$txt);
        libxml_disable_entity_loader(true);
        $res = json_decode(json_encode(simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        writeLog('jsapi_notify',$res);
        $key = '9157e3a3e5c147b84d1b6f6b48f823e5';
        if(!is_array($res)){
            writeLog('jsapi_notify','数据错误');die;
        }
        if($res['sign']!=MakeSign($res,$key)){
            writeLog('jsapi_notify','签名错误');die;
        }
        if($res['return_code']!='SUCCESS'){
            writeLog('jsapi_notify','返回状态不正确');die;
        }
        // 启动事务
        Db::startTrans();
        try {
            Db::name('admin_admins')->where(['two_prepay_id'=>'1'])->update(['two_isrecharge'=>1]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            writeLog('jsapi_notify',$e->getMessage());
            // 回滚事务
            Db::rollback();
            die;
        }
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
    }
}